/*
 * test_lrucache.c
 *
 *  Created on: Feb 21, 2011
 *      Author: thienlong
 */

#include "../src/cache.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
lru_cache *cache = NULL;
val loader(key k, mem_storage *storage, void *arg) {
	unsigned int temp;
	int *v = malloc(sizeof(int));
	*v = rand_r(&temp);
	return (val) v;
}

void *pthread_hanlder(void* arg) {
	unsigned int temp;
	int id = (size_t) arg;
	int i = 0;
	while (true) {
		++i;
		if (i == 26) {
			sleep(1);
		}
		int *k = malloc(sizeof(int));
		*k = rand_r(&temp) % 16;
		int* v = lru_cache_get(cache, k, id);
		printf("key/val = %d/%d\n", *k, *v);
		fflush(stdout);
		sleep(1);
	}
}

int main(int argc, char **argv) {
	map_fn fns = {str_hash_fn, str_eq_fn, mem_free, mem_free};
	cache = lru_cache_new(&fns, 1024, 16, loader, NULL, 64, NULL);
	pthread_t id = 0;
	pthread_create(&id, NULL, pthread_hanlder, (void *) 1);
	pthread_create(&id, NULL, pthread_hanlder, (void *) 2);
	while (true) {
			sleep(1000);
		}
}

